package org.jeecg.modules.relation.relteachersubject.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.common.baseservice.IBaseBatchService;
import org.jeecg.modules.relation.relbuildingclassroom.vo.RelBuildingClassroomVo;
import org.jeecg.modules.relation.relteachersubject.entity.RelTeacherSubject;
import org.jeecg.modules.relation.relteachersubject.mapper.RelTeacherSubjectMapper;
import org.jeecg.modules.relation.relteachersubject.service.IRelTeacherSubjectService;
import org.jeecg.modules.relation.relteachersubject.vo.RelTeacherSubjectVO;
import org.jeecg.modules.subject.entity.CouSubject;
import org.jeecg.modules.teacher.entity.TeaTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.security.auth.Subject;
import java.util.*;

/**
 * @Description: rel_teacher_subject
 * @author: baochangrong
 * @Date: 2022-02-16
 * @Version: V1.0
 */
@Service
public class RelTeacherSubjectServiceImpl extends ServiceImpl<RelTeacherSubjectMapper, RelTeacherSubject> implements IRelTeacherSubjectService {

    @Autowired
    private RelTeacherSubjectMapper relTeacherSubjectMapper;
    @Autowired
    private IBaseBatchService entityBatchOperate;

    @Override
    public List<RelTeacherSubjectVO> queryAllTeacher(String tenantId) {
        return relTeacherSubjectMapper.queryAllTeacher(tenantId);
    }

    @Override
    public List<RelTeacherSubjectVO> queryAllSubject(String tenantId) {
        return relTeacherSubjectMapper.queryAllSubject(tenantId);
    }

    @Override
    public IPage<RelTeacherSubjectVO> queryVo(Page page, RelTeacherSubject relTeacherSubject) {
        List<RelTeacherSubjectVO> relTeacherSubjectVOList = relTeacherSubjectMapper.queryVo(relTeacherSubject);
        Integer total = relTeacherSubjectVOList.size();
        IPage<RelTeacherSubjectVO> result = new Page<>(page.getCurrent(), page.getSize(), total);
        result.setRecords(relTeacherSubjectVOList);
        return result;
    }

    @Override
    public List<RelTeacherSubject> queryRelTeacherSubjectByTeacherIds(Collection<String> teaTeacherIds) {
        LambdaQueryWrapper<RelTeacherSubject> lambdaQueryWrapper = new LambdaQueryWrapper<RelTeacherSubject>();
        lambdaQueryWrapper.in(RelTeacherSubject::getTeacherId,teaTeacherIds);
        return list(lambdaQueryWrapper);
    }
    @Override
    public List<RelTeacherSubject> queryRelTeacherSubjectBySubjectIds(Collection<String> subjectIds) {
        LambdaQueryWrapper<RelTeacherSubject> lambdaQueryWrapper = new LambdaQueryWrapper<RelTeacherSubject>();
        lambdaQueryWrapper.in(RelTeacherSubject::getSubjectId,subjectIds);
        return list(lambdaQueryWrapper);
    }
    @Override
    public List<RelTeacherSubjectVO> queryTeacherListBySubject(RelTeacherSubjectVO relTeacherSubjectVO) {
        return relTeacherSubjectMapper.queryTeacherListBySubject(relTeacherSubjectVO);
    }

    /**
     * 用教师档案更新任教科目关系表
     * @param teaTeacherId   老师id
     * @param teacherSubjectIds   老师任教科目ids
     * @return
     */
    @Override
    public boolean batchUpdateDeleteByTeacherId(String teaTeacherId,Collection<String> teacherSubjectIds){
        if (oConvertUtils.isEmpty(teaTeacherId) || teacherSubjectIds.isEmpty() ){
            return true;
        }
        //查一下跟已有的是否一样，一样则不动(避免更新其他字段时候随意删除更新)，不一样则删除后重新插入
        List<RelTeacherSubject> list = queryRelTeacherSubjectByTeacherIds( Arrays.asList(new String[]{teaTeacherId}));
        Collection<String> relSubjects = new HashSet<String>();
        for (RelTeacherSubject relTeacherSubjectOne : list
             ) {
            relSubjects.add(relTeacherSubjectOne.getSubjectId());
        }

        if(teacherSubjectIds.containsAll(relSubjects) && relSubjects.containsAll(teacherSubjectIds)){
            return  true;
        } else {
            //更新关系：1.删除后增加  2.找到已有数据，修改相同数据+插入新增数据+删除减少数据
            //1.批量删除
            Map<String, String> whereConditionMap = new HashMap<>();
            whereConditionMap.put("teacherId", teaTeacherId);
            UpdateWrapper<RelTeacherSubject> updateWrapper = new UpdateWrapper<RelTeacherSubject>();
            updateWrapper = entityBatchOperate.getBatchSqlUpdateByMap(updateWrapper,RelTeacherSubject.class,null,whereConditionMap);
            this.remove(updateWrapper);
            //2.批量插入

                List<RelTeacherSubject> relTeacherSubjectList = new ArrayList<RelTeacherSubject>();
                for (String subjectId : teacherSubjectIds
                ) {
                    RelTeacherSubject relTeacherSubject = new RelTeacherSubject();
                    relTeacherSubject.setTeacherId(teaTeacherId);
                    relTeacherSubject.setSubjectId(subjectId);
                    //relTeacherSubject.setRelStatus(1); //设置关系状态为：1有效
                    //relTeacherSubject.setTenantId(teaTeacher.getTenantId());  //自动设置
                    //save(relTeacherSubject);
                    relTeacherSubjectList.add(relTeacherSubject);
                }
                //批量插入
                return saveBatch(relTeacherSubjectList);


        }
    }

    /**
     * 用科目id更新教师科目关系
     * @param subjectId
     * @param subjectTeacherIds
     * @return
     */
    public boolean batchUpdateDeleteBySubjectId(String subjectId,Collection<String> subjectTeacherIds){
        if (oConvertUtils.isEmpty(subjectId) || subjectTeacherIds.isEmpty() ){
            return true;
        }
        //查一下跟已有的是否一样，一样则不动(避免更新其他字段时候随意删除更新)，不一样则删除后重新插入

        List<RelTeacherSubject> list = queryRelTeacherSubjectBySubjectIds(Arrays.asList(new String[]{subjectId}));
        Collection<String> relTeachers = new HashSet<String>();
        for (RelTeacherSubject relTeacherSubjectOne : list
        ) {
            relTeachers.add(relTeacherSubjectOne.getTeacherId());
        }

        if(subjectTeacherIds.containsAll(relTeachers) && relTeachers.containsAll(subjectTeacherIds)){
            return  true;
        } else {
            //更新关系：1.删除后增加  2.找到已有数据，修改相同数据+插入新增数据+删除减少数据
            //1.批量删除
            Map<String, String> whereConditionMap = new HashMap<>();
            whereConditionMap.put("subjectId", subjectId);
            UpdateWrapper<RelTeacherSubject> updateWrapper = entityBatchOperate.batchCopyUpdateByWhereMap(whereConditionMap, RelTeacherSubject.class);
            this.remove(updateWrapper);

            //2.批量插入
                List<RelTeacherSubject> relTeacherSubjectList = new ArrayList<RelTeacherSubject>();
                for (String teacherId : subjectTeacherIds
                ) {
                    RelTeacherSubject relTeacherSubject = new RelTeacherSubject();
                    relTeacherSubject.setTeacherId(teacherId);
                    relTeacherSubject.setSubjectId(subjectId);
//                    relTeacherSubject.setRelStatus(1); //设置关系状态为：1有效  //自动设置
                    //relTeacherSubject.setTenantId(teaTeacher.getTenantId()); //自动设置
                    //save(relTeacherSubject);
                    relTeacherSubjectList.add(relTeacherSubject);
                }
                //批量插入
                return saveBatch(relTeacherSubjectList);

            }

    }
}
